package com.sztx.pay.center.tool;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.tools.ant.filters.TokenFilter.Trim;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sztx.commons.httpclient.HttpClientConfigBuilder;
import com.sztx.commons.httpclient.HttpClientUtil;
import com.sztx.commons.httpclient.HttpHeaderBuilder;

public class MqSend {
	private static final Logger logger = LoggerFactory.getLogger(MqSend.class);	

	public static String PUBLISH_URL = "http://rabbitmq.berbon.com/api/exchanges/websystem/amq.default/publish";
		
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new FileReader("D:/20170328/sendMq.txt"));
		
		String readLine = null;
		int i = 0;
		int s = 0;
		int m = 1;
		while ((readLine = bf.readLine()) != null) {// 使用readLine方法，一次读一行	
			if(++i <= s){
				continue;
			}
			if(i > m){
				break;
			}
			try {
				TimeUnit.MILLISECONDS.sleep(200l);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			readLine = readLine.trim();
			String result = getHtmlContent(PUBLISH_URL, readLine);
			String trueResult = "{\"routed\":true}";
			System.out.println("处理第"+i+"条数据,结果为:"+result);
			if(!trueResult.equals(result)){
				System.err.println("提交mq失败");
				return;
			}
			
			//return;
		}
	}
	
	
	private static String getHtmlContent(String url,String data) throws IOException{	
		HttpClientConfigBuilder clientBuilder = HttpClientConfigBuilder.custom();
		HttpHeaderBuilder headerBuilder = HttpHeaderBuilder.custom().authorization("Basic YWRtaW46NWM3NjgyZDZkZQ==");
		String encoding = "UTF-8";
		data = data.replace("\"", "\\\"");
		String str = "{\"vhost\":\"websystem\",\"name\":\"amq.default\",\"properties\":{\"delivery_mode\":1,\"headers\":{}},\"routing_key\":\"queue.websystem.paycenter\",\"delivery_mode\":\"1\",\"payload\":\""+data+"\",\"headers\":{},\"props\":{},\"payload_encoding\":\"string\"}";
		return HttpClientUtil.post(clientBuilder, url, str, headerBuilder, encoding);
	}
	
	
	
}
